package org.ipower.examsystem.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ipower.examsystem.dao.CooperationCatalogDao;
import org.ipower.examsystem.domain.CooperationCatalog;
import org.ipower.examsystem.service.CooperationCatalogInfo;
import org.ipower.examsystem.service.CooperationCatalogService;
import org.ipower.platform.domain.Pager;
import org.ipower.platform.domain.QueryCondition;
import org.ipower.platform.service.TreeCheckedDataCollection;

/**
 * 合作科目服务实现类。
 * @author jeasonyoung
 */
public class CooperationCatalogServiceImpl  implements CooperationCatalogService{
    private static final long serialVersionUID = 1L;
    private CooperationCatalogDao dao;
    
    @Override
    public void setCooperationCatalogDao(CooperationCatalogDao dao) {
        this.dao = dao;
    } 
   
    @Override
    public Pager<CooperationCatalogInfo> list(String cooperationName, String catalogName, int start, int limit) {
        Pager<CooperationCatalogInfo> result = new Pager<CooperationCatalogInfo>();
         List<QueryCondition> where = new ArrayList<QueryCondition>();
         if(cooperationName != null && !cooperationName.isEmpty()){
            where.add(new QueryCondition("CooperationName", cooperationName, QueryCondition.MODE_ANY));
         }
         if(catalogName != null && !catalogName.isEmpty()){
             where.add(new QueryCondition("CatalogName", catalogName, QueryCondition.MODE_ANY));
         }
        List<CooperationCatalog> list = this.dao.loadAll(where);
        if(list != null && list.size() > 0){
            Map<String,CooperationCatalogInfo> map = new HashMap<String, CooperationCatalogInfo>();
            for(CooperationCatalog cc : list){
                CooperationCatalogInfo info =  map.get(cc.getCooperationID());
                if(info == null){
                    info = new CooperationCatalogInfo();
                    info.setCooperationID(cc.getCooperationID());
                    info.setCooperationName(cc.getCooperationName());
                    info.setCatalogName(cc.getCatalogName());
                    map.put(info.getCooperationID(), info);
                }else{
                    info.setCatalogName(info.getCatalogName() + "," + cc.getCatalogName());
                }
            } 
            List<CooperationCatalogInfo> dataList = new ArrayList<CooperationCatalogInfo>();
            int index = 0;
            for(CooperationCatalogInfo cci : map.values()){
                if(start >= index && limit > 0){
                    dataList.add(cci);
                    limit--;
                }
                index++;
            }
            result.setEntityList(dataList);
            result.setTotalCounts(map.size());
        }
      return result;
    }
   
    @Override
    public void delete(String cooperationID) {
        this.dao.delete(cooperationID);
    }
   
    @Override
    public void update(String cooperationID, String[] catalogID) {
        this.dao.update(cooperationID, catalogID);
    }
   
    @Override
    public TreeCheckedDataCollection CreateCatalogTree(String cooperationID) {
      return this.dao.CreateCatalogTree(cooperationID);
    }
}